﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F5LocalTroubles : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F5LocalTroubles()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4LocalTroubles.DoubleClick += F4LocalTroubles_DoubleClick;
            this.F4LocalTroubles.PopupMenuShowing += F4LocalTroubles_PopupMenuShowing;
            this.f5F5LocalTroublesContextMenu.ItemClicked += f4LocalTroublesContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = F4LocalTroubles.GetFocusedDataRow();
            frm_Edit_F5LocalTroubles editForm = new frm_Edit_F5LocalTroubles();
            editForm.troubleID = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
        }

        void f4LocalTroublesContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f5F5LocalTroublesContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F5LocalTroubles add_Form = new frm_Add_F5LocalTroubles();
                    add_Form.ShowDialog();
                    LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4LocalTroubles_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f5F5LocalTroublesContextMenu.Show(MousePosition);
        }

        private void F4LocalTroubles_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT t1.ID, t1.[CheckNo], t1.[CustomerID], t1.Date, t1.[ReportNo],"
                 + " CAST(DATENAME(MONTH, t1.Date) AS VARCHAR(3)) + '-' + CAST(DATEPART(yy, t1.Date)%100 AS VARCHAR(MAX)) AS Month,"
                 + " t1.TimePlanNo AS OrderNo, t1.SerialNo, t2.PartNo,"
                 + " t6.CompName, t1.Kind, t1.[ExportQuantity], t1.[NGQuantity],"
                 + " CAST((ROUND((t1.[NGQuantity]/t1.[ExportQuantity]), 4) * 100) AS VARCHAR(MAX)) + '%' AS NGPercent,"
                 + " t1.F4TroubleContent, t1.[NGCode], t3.StaffName AS Checker, t1.[ChangeDate], t4.StaffName AS MadeBy,"
                 + " t1.[TroubleKind], t1.Reason, t1.[ReasonCode], t1.Solution, t5.StaffName AS ResponsePerson,"
                 + " t1.[ReportDate], t1.[ReturnDeadline], t1.[FinishedDate], t1.Remark, t1.AssDate, t1.AssResult,"
                 + " t1.AssPerson, t1.AssNote, DATEPART(yy, t1.AssDate) AS Year"
                 + " FROM [231_F5Troubles] AS t1 LEFT OUTER JOIN"
                 + " [231_F5Plan] AS t2 ON t2.OrderNo=t1.TimePlanNo LEFT OUTER JOIN"
                 + " [222_Staff] AS t3 ON t3.StaffID=t1.Checker LEFT OUTER JOIN"
                 + " [222_Staff] AS t4 ON t4.StaffID=t1.[MadeBy] LEFT OUTER JOIN"
                 + " [222_Staff] AS t5 ON t5.StaffID=t1.[ResponsePerson] LEFT OUTER JOIN"
                 + " [231_Comp] AS t6 ON t2.PartNo=t6.CompID"
                 + " WHERE t1.Type=0"
                 + " ORDER BY t1.Date";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4LocalTroubles.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(string));
                newTable.Columns.Add("CheckNo", typeof(string));
                newTable.Columns.Add("CustomerID", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("ReportNo", typeof(string));
                newTable.Columns.Add("Month", typeof(string));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("CompName", typeof(string));
                newTable.Columns.Add("Kind", typeof(string));
                newTable.Columns.Add("ExportQuantity", typeof(float));
                newTable.Columns.Add("NGQuantity", typeof(float));
                newTable.Columns.Add("NGPercent", typeof(string));
                newTable.Columns.Add("F4TroubleContent", typeof(string));
                newTable.Columns.Add("NGCode", typeof(string));
                newTable.Columns.Add("Checker", typeof(string));
                newTable.Columns.Add("ChangeDate", typeof(DateTime));
                newTable.Columns.Add("MadeBy", typeof(string));
                newTable.Columns.Add("TroubleKind", typeof(string));
                newTable.Columns.Add("Reason", typeof(string));
                newTable.Columns.Add("ReasonCode", typeof(string));
                newTable.Columns.Add("Solution", typeof(string));
                newTable.Columns.Add("ResponsePerson", typeof(string));
                newTable.Columns.Add("ReportDate", typeof(DateTime));
                newTable.Columns.Add("ReturnDeadline", typeof(DateTime));
                newTable.Columns.Add("FinishedDate", typeof(DateTime));
                newTable.Columns.Add("Remark", typeof(string));
                newTable.Columns.Add("AssDate", typeof(DateTime));
                newTable.Columns.Add("AssResult", typeof(string));
                newTable.Columns.Add("AssPerson", typeof(string));
                newTable.Columns.Add("AssNote", typeof(string));
                newTable.Columns.Add("AssYear", typeof(string));
                gridF4LocalTroubles.DataSource = newTable;
            }
            F4LocalTroubles.Columns[0].Caption = "ID";
            F4LocalTroubles.Columns[1].Caption = "Số báo sản phẩm lỗi";
            F4LocalTroubles.Columns[2].Caption = "Khách hàng";
            F4LocalTroubles.Columns[3].Caption = "Ngày phát hiện";
            F4LocalTroubles.Columns[4].Caption = "Report No.";
            F4LocalTroubles.Columns[5].Caption = "Month";
            F4LocalTroubles.Columns[6].Caption = "Order No.";            
            F4LocalTroubles.Columns[7].Caption = "Serial No.";
            F4LocalTroubles.Columns[8].Caption = "Part No.";
            F4LocalTroubles.Columns[9].Caption = "Part Name";
            F4LocalTroubles.Columns[10].Caption = "Kind (New/Old)";
            F4LocalTroubles.Columns[11].Caption = "Production Q'ty (PC)";            
            F4LocalTroubles.Columns[12].Caption = "NG Q'ty (PC)";
            F4LocalTroubles.Columns[13].Caption = "% NG (PC)";
            F4LocalTroubles.Columns[14].Caption = "Nội dung lỗi";
            F4LocalTroubles.Columns[15].Caption = "Mã lỗi";
            F4LocalTroubles.Columns[16].Caption = "Người kiểm tra";
            F4LocalTroubles.Columns[17].Caption = "Ngày yêu cầu đổi hàng";
            F4LocalTroubles.Columns[18].Caption = "Người gây lỗi";
            F4LocalTroubles.Columns[19].Caption = "Kiểu lỗi";
            F4LocalTroubles.Columns[20].Caption = "Nguyên nhân";
            F4LocalTroubles.Columns[21].Caption = "Mã nguyên nhân";
            F4LocalTroubles.Columns[22].Caption = "Biện pháp khắc phục";
            F4LocalTroubles.Columns[23].Caption = "Người phụ trách";
            F4LocalTroubles.Columns[24].Caption = "Ngày viết báo cáo";
            F4LocalTroubles.Columns[25].Caption = "Thời hạn trả hàng";
            F4LocalTroubles.Columns[26].Caption = "Ngày hoàn thành";
            F4LocalTroubles.Columns[27].Caption = "Ghi chú";
            F4LocalTroubles.Columns[28].Caption = "NGày đánh giá";
            F4LocalTroubles.Columns[29].Caption = "Kết quả";
            F4LocalTroubles.Columns[30].Caption = "Người đánh giá";
            F4LocalTroubles.Columns[31].Caption = "Nhận xét";
            F4LocalTroubles.Columns[32].Caption = "Year";
            F4LocalTroubles.Columns[0].Visible = false;
            F4LocalTroubles.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {            
            frm_Add_F5LocalTroubles add_Form = new frm_Add_F5LocalTroubles();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }
    }
}
